 <#-- 
 
 crea una clase para c# para la tabla  ${table}...
 Author : Luis Molina...
 
 -->
<#-- variables used internally by the generator... -->
<#assign nameFile="${table}">
<#assign extensionFile=".cs">
<#assign languageGenerated="c#">
<#assign description="c#">
<#assign targetDirectory="clases">
<#assign appliesToAllTables="true">
<#-- END variables used internally by the generator... -->


Imports Microsoft.VisualBasic

Imports System
Imports System.Data
Imports System.Collections.Generic

Imports System.Text
Imports System.Io

  Public Class ${table}
  


#Region "Variables"
	<#list table.getFields() as field>		
		 <#-- segun el tipo de campo...   -->
		 <#switch field.getType()>
			  <#case "_int">
			     Private s_${field} As Integer
			     <#break>
			  <#case "_string">
			     Private s_${field} As New String("")
			     <#break>
			  <#case "_date">
			     Private s_${field} As Date
			     <#break>
			  <<#case "_boolean">
			     Private s_${field} As Boolean
			     <#break>
			  <#default>
			     Private s_${field} As New String("")
		</#switch>  	 
	</#list>
    
 #End Region 




#Region "Propiedades"
	<#list table.getFields() as field>		
		 <#-- segun el tipo de campo...   -->
		 <#switch field.getType()>
			  <#case "_int">
			    Public Property ${field}() As Integer
					Get
						Return s_${field}
					End Get
					Set(ByVal Value As Integer)
						s_${field} = Value
					End Set
				End Property
			     <#break>
			  <#case "_string">
			    Public Property ${field}() As String
					Get
						Return s_${field}
					End Get
					Set(ByVal Value As String)
						s_${field} = Value
					End Set
				End Property
			     <#break>
			  <#case "_date">
			    Public Property ${field}() As Date
					Get
						Return s_${field}
					End Get
					Set(ByVal Value As Date)
						s_${field} = Value
					End Set
				End Property
			     <#break>
			  <<#case "_boolean">
			     	Public Property ${field}() As Boolean
						Get
							Return s_${field}
						End Get
						Set(ByVal Value As Boolean)
							s_${field} = Value
						End Set
					End Property
			     <#break>
			  <#default>
			    Public Property ${field}() As String
					Get
						Return s_${field}
					End Get
					Set(ByVal Value As String)
						s_${field} = Value
					End Set
				End Property
		</#switch>  	 
	</#list>
   
  
   
#End Region


 ' devuelve un arraylist de ...
    Public shared Function getArrayList() As ArrayList
        Dim db As New dbClass(ctes.conStringAdoSvAdmin)
        Dim sql As String
        Dim reg As Object
         

        Try
            sql = "select * from ${table}"
            reg = db.sql(sql)
            Do While reg.read()
                Dim pp As New ${table}
                
				<#list table.getFields() as field>		
				 <#-- segun el tipo de campo...   -->
				 <#switch field.getType()>
					  <#case "_int">
						 pp.${field} = sf.Entero(reg("${field}"))
						 <#break>
					  <#case "_string">
						 pp.${field} = sf.Cadena(reg("${field}"))
						 <#break>
					  <#case "_date">
						 pp.${field} = sf.Fecha(reg("${field}"))
						 <#break>
					  <<#case "_boolean">
						 pp.${field} = sf.Bool(reg("${field}"))
						 <#break>
					  <#default>
						 pp.${field} = sf.Cadena(reg("${field}"))
				</#switch>  	 
			</#list>
	 
                
                getArrayList.Add(pp)
                pp = Nothing
            Loop
            reg.close()
        Catch ex As Exception
        Finally
            db.dispose()
        End Try
        
    End Function


 ' devuelve un List de ...
    Public shared Function getList() As List (of ${table})
        Dim db As New dbClass(ctes.conStringAdoSvAdmin)
        Dim sql As String
        Dim reg As Object
        Dim lista as new List (of ${table})
        
        Try
            sql = "select * from ${table}"
            reg = db.sql(sql)
            Do While reg.read()
                Dim pp As New ${table}
        	<#list table.getFields() as field>		
				 <#-- segun el tipo de campo...   -->
				 <#switch field.getType()>
					  <#case "_int">
						 pp.${field} = sf.Entero(reg("${field}"))
						 <#break>
					  <#case "_string">
						 pp.${field} = sf.Cadena(reg("${field}"))
						 <#break>
					  <#case "_date">
						 pp.${field} = sf.Fecha(reg("${field}"))
						 <#break>
					  <<#case "_boolean">
						 pp.${field} = sf.Bool(reg("${field}"))
						 <#break>
					  <#default>
						 pp.${field} = sf.Cadena(reg("${field}"))
				</#switch>  	 
			</#list>
                
                lista.Add(pp)
                pp = Nothing
            Loop
            reg.close()
        Catch ex As Exception
        Finally
            db.dispose()
        End Try
        return lista
        lista=nothing
        
    End Function


 Public shared Function update  (Byval ${table}x As ${table}) As Boolean
        Dim db As New dbClass(ctes.conStringAdoSvAdmin)
        Dim sqlt As String
        Try

          sqlt = " update ${table} set  "
		  <#assign count=0>
		  <#list table.getFields() as field>		
				 <#-- segun el tipo de campo...   -->
				 <#switch field.getType()>
					  <#case "_int">
						 sqlt += " ${field}=" & sf.cadena(${table}x.${field})  & "<#if (table.getNumberOfFields() -  count  != 1)>,</#if>"
						 <#break>
					  <#case "_string">
						 sqlt += " ${field}='" & sf.cadena(${table}x.${field}) & "'<#if (table.getNumberOfFields() -  count  != 1)>,</#if> " 
						 <#break>
					  <#case "_date">
						 sqlt += " ${field}='" & sf.fechaSql(${table}x.${field}) & "'<#if (table.getNumberOfFields() -  count  != 1)>,</#if> " 
						 <#break>
					  <<#case "_boolean">
						 sqlt += " ${field}=" & sf.Entero(${table}x.${field}).toString()  & "<#if (table.getNumberOfFields() -  count  != 1)>,</#if>"
						 <#break>
					  <#default>
						 sqlt += " ${field}='" & sf.cadena(${table}x.${field}) & "'<#if (table.getNumberOfFields() -  count  != 1)>,</#if> " 
				</#switch>  	
				<#assign count = count+1>
			</#list>
           
           
            sqlt += " where ${table.getKey()} =" & ${table}x.${table.getKey()}
            db.ejecutar(sqlt)
           return true
        Catch ex As Exception
        	return false
        
        Finally
            db.dispose()
        End Try
    End Function



Public shared Function get${table} (ByVal ${table.getKey()}x As integer) As ${table}
        dim salida as new ${table}
        Dim db As New dbClass(ctes.conStringAdoSvAdmin)
        Dim reg As Object
        Dim sqlt As String

        Try
            sqlt = " select * from ${table} where ${table.getKey()}=" & ${table.getKey()}x

            reg = db.sql(sqlt)
            If reg.Read() Then
              
            <#list table.getFields() as field>		
				 <#-- segun el tipo de campo...   -->
				 <#switch field.getType()>
					  <#case "_int">
						 salida.${field} = sf.Entero(reg("${field}"))
						 <#break>
					  <#case "_string">
						 salida.${field} = sf.Cadena(reg("${field}"))
						 <#break>
					  <#case "_date">
						 salida.${field} = sf.Fecha(reg("${field}"))
						 <#break>
					  <<#case "_boolean">
						 salida.${field} = sf.Bool(reg("${field}"))
						 <#break>
					  <#default>
						 salida.${field} = sf.Cadena(reg("${field}"))
				</#switch>  	 
			</#list>
              
              
            End If
            reg.close()
        Catch ex As Exception
        Finally
            db.dispose()
        End Try
		return salida
    End Function

' devuelve un arraylist de...
    Public shared Function getArray${table}() As ${table}()
        Dim av() As ${table}
        Dim db As New dbClass(ctes.conStringAdoSvAdmin)
        Dim reg As Object
        Dim sqlt As String
        Dim contador As Integer
        contador = 0

        Try
            sqlt = " select * from ${table} "

            reg = db.sql(sqlt)
                do while reg.read()
                Dim pp As New ${table}
                
			<#list table.getFields() as field>		
				 <#-- segun el tipo de campo...   -->
				 <#switch field.getType()>
					  <#case "_int">
						 pp.${field} = sf.Entero(reg("${field}"))
						 <#break>
					  <#case "_string">
						 pp.${field} = sf.Cadena(reg("${field}"))
						 <#break>
					  <#case "_date">
						 pp.${field} = sf.Fecha(reg("${field}"))
						 <#break>
					  <<#case "_boolean">
						 pp.${field} = sf.Bool(reg("${field}"))
						 <#break>
					  <#default>
						 pp.${field} = sf.Cadena(reg("${field}"))
				</#switch>  	 
			</#list>
                
                ReDim Preserve av(contador)
                av.SetValue(pp, contador)
                contador+=1
                pp = Nothing
            loop
           
        Catch ex As Exception
        Finally
            db.dispose()
        End Try
        Return av
    End Function

   Public Function readDb(ByVal ${table.getKey()}x As integer) As boolean
        Dim db As New dbClass(ctes.conStringAdoSvAdmin)
        Dim reg As Object
        Dim sqlt As String

        Try
            sqlt = " select * from ${table} where ${table.getKey()}=" & ${table.getKey()}x

            reg = db.sql(sqlt)
            If reg.Read() Then
              
                      <#list table.getFields() as field>		
				 <#-- segun el tipo de campo...   -->
				 <#switch field.getType()>
					  <#case "_int">
						 Me.${field} = sf.Entero(reg("${field}"))
						 <#break>
					  <#case "_string">
						 Me.${field} = sf.Cadena(reg("${field}"))
						 <#break>
					  <#case "_date">
						 Me.${field} = sf.Fecha(reg("${field}"))
						 <#break>
					  <<#case "_boolean">
						 Me.${field} = sf.Bool(reg("${field}"))
						 <#break>
					  <#default>
						 Me.${field} = sf.Cadena(reg("${field}"))
				</#switch>  	 
			</#list>
              
            End If
            reg.close()
        Catch ex As Exception
        Finally
            db.dispose()
        End Try
    End Function
    
    
       ' sirve para comprobar que hay un ${table} con el mismo id ...
    Public shared Function exists (ByVal ${table}Obj as ${table}) As Boolean
        Dim db As New dbClass(ctes.conStringAdoSvAdmin)
        Dim reg As Object
        Dim sqlt As String
        Dim salida As Boolean
        Try
            sqlt = " select * from ${table}  where ${table.getKey()} <> 0 and ( "
            
                    
            sqlt += " )"
           
            reg = db.sql(sqlt)
            salida = reg.read()

        Catch ex As Exception
        Finally
            db.dispose()
        End Try
        Return salida
    End Function

 
  Public shared Function insert (byval obj${table} as ${table}) As String
        Dim db As New dbClass(ctes.conStringAdoSvAdmin)

        Dim reg As Object
        Dim sqlt As String
        Try

            sqlt = " insert into ${table} (${table.getlistWithAllFieldsExceptKeys()} "
            sqlt += " ) values ("

		  <#assign count=0>
		  <#list table.getFields() as field>		
				 <#-- segun el tipo de campo...   -->
				 <#switch field.getType()>
					  <#case "_int">
						 sqlt += " " & obj${table}.${field}.ToString &"<#if (table.getNumberOfFields() -  count  != 1)>,</#if>"
						 <#break>
					  <#case "_string">
						 sqlt += "'" & sf.cadena(obj${table}.${field}) & "' <#if (table.getNumberOfFields() -  count  != 1)>,</#if>"
						 <#break>
					  <#case "_date">
						 sqlt += " '" & sf.FechaSql(obj${table}.${field}) & "' <#if (table.getNumberOfFields() -  count  != 1)>,</#if>"
						 <#break>
					  <<#case "_boolean">
						 sqlt += " " & sf.Entero(obj${table}.${field}).ToString &"<#if (table.getNumberOfFields() -  count  != 1)>,</#if>"
						 <#break>
					  <#default>
						 sqlt += "'" & sf.cadena(obj${table}.${field}) & "' <#if (table.getNumberOfFields() -  count  != 1)>,</#if>"
				</#switch>  	
				<#assign count = count+1>
			</#list>			
                      
			 sqlt += " )"

            db.ejecutar(sqlt)
            reg = db.sql("select @@identity as id from ${table}")
            If reg.Read() Then
                return reg("id").ToString
            Else
                return ""
            End If
            reg.close()

           
        Catch ex As Exception
        Finally
            db.dispose()
        End Try


    End Function 'insertarEnBd()






 <#assign x=0>
<#list table.getRelations() as relation>

  
    <#assign x = x+1>
 
        public static ${table} get${table}By${relation.getParentKey()}(int ${relation.getParentKey()})
        {
            ${table} ${table}x = new ${table}();
            MySqlDataReader reg;
            string sqlt;
                ${table} obj${table} = new ${table}();


            dbClass db = new dbClass(ctes.conStringAdoGeneral);
            try
            {
            sqlt = "select * from ${relation.getChildTable()}";
          
            sqlt += " where ${relation.getParentKey()}=" + sf.cadena(${relation.getParentKey()});  

            reg = db.sql(sqlt);
            if (reg.Read())
            {
          
              <#list table.getFields() as field>

                         <#-- segun el tipo de campo...   -->
                         <#switch field.getType()>
                                  <#case "_int">
                                     obj${table}.${field} = sf.entero(reg["${field}"].ToString());
                                     <#break>
                                  <#case "_string">
                                     obj${table}.${field} = sf.cadena(reg["${field}"].ToString());	  					
                                     <#break>
                                  <#case "_date">
                                    obj${table}.${field} = sf.fecha(reg["${field}"].ToString());
                                     <#break>
                                  <#case "_boolean">
                                     obj${table}.${field} = sf.Bool(reg["${field}"].ToString());
                                     <#break>
                                  <#default>
                                     obj${table}.${field} = sf.cadena(reg["${field}"].ToString());	  					
                        </#switch>  

                </#list>		 
	  			 
                
           
               }
            reg.Close();
            }
            catch (Exception ex)
            {
                // Argument is optional, no "When" keyword 
            }
            finally
            {
                db.Dispose();
              
            }
            return  obj${table} ;
       }
    
	
	 public void delete${relation.getParentTable()}By${relation.getParentKey()} (int ${relation.getParentKey()}x)
	 {
		 dbClass db=new  dbClass(ctes.conStringAdoGeneral);
		 String  sqlt;
		 
		try
		{
		  sqlt = " delete from ${table} where ${relation.getParentKey()}=" + ${relation.getParentKey()}x ;
			db.ejecutar(sqlt);
		}
		finally
		{db.Dispose();}
		  

	}  

</#list>
 
 
 

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub



    Public Sub New()

    End Sub


    Public Sub new (ByVal ${table.getKey()}x As integer)
            Dim db As New dbClass(ctes.conStringAdoSvAdmin)
            Dim reg As Object
            Dim sqlt As String

            Try
                sqlt = " select * from ${table} where ${table.getKey()}=" & ${table.getKey()}x

                reg = db.sql(sqlt)
                If reg.Read() Then
                  
                               <#list table.getFields() as field>		
				 <#-- segun el tipo de campo...   -->
				 <#switch field.getType()>
					  <#case "_int">
						 Me.${field} = sf.Entero(reg("${field}"))
						 <#break>
					  <#case "_string">
						 Me.${field} = sf.Cadena(reg("${field}"))
						 <#break>
					  <#case "_date">
						 Me.${field} = sf.Fecha(reg("${field}"))
						 <#break>
					  <<#case "_boolean">
						 Me.${field} = sf.Bool(reg("${field}"))
						 <#break>
					  <#default>
						 Me.${field} = sf.Cadena(reg("${field}"))
				</#switch>  	 
			</#list>
              
            End If
            reg.close()
        Catch ex As Exception
        Finally
            db.dispose()
        End Try
    End Sub



End Class